package small.common.core.spi.extension.factory;

import small.common.core.spi.Spi;
import small.common.core.spi.extension.ExtensionFactory;
import small.common.core.spi.extension.ExtensionLoader;

/**
 * Spi拓展工厂
 *
 * @author jinzhou
 * @data 2023/4/13 14:37
 */
public class SpiExtensionFactory implements ExtensionFactory {

    @Override
    public <T> T getExtension(Class<T> type, String name) {
        if (type.isInterface() && type.isAnnotationPresent(Spi.class)) {
            //获取指定类型加载器
            ExtensionLoader<T> loader = ExtensionLoader.getExtensionLoader(type);
            if (!loader.getSupportedExtensions().isEmpty()) {
                return loader.getAdaptiveExtension();
            }
        }
        return null;
    }
}
